/*
 * Copyright (C) 2010 Fred Bojin Liu.
 *
 * Licensed under the GPL, Version 2.0;
 * you may not use this file except in compliance with the License.
 * A copy of the license is included (see the "LICENSE.txt")
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package edu.ucdavis.fredliu.speedtracker;

import java.util.Date;

import org.achartengine.model.XYSeries;

import android.location.Location;
import android.location.LocationListener;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapController;
import com.google.android.maps.OverlayItem;

public class LocationUpdateHandler implements LocationListener {
	private MapController mMapController;
	private CarItemizedOverlay mOverlay;
	private MocGPS mainAct;

	public LocationUpdateHandler(MapController c, CarItemizedOverlay overlay, MocGPS activity) {
		mMapController = c;
		mOverlay = overlay;
		mainAct = activity;
	}

	public void onLocationChanged(Location location) {
		//create and store Record
		Record record = Record.createRecord(location);
		mainAct.getTrace().add(record);
		
		//display time, speed, location info in TextViews
		Date date =new Date(record.getTime());
		((TextView)mainAct.findViewById(R.id.TimeNum)).setText(date.toString());
		
		((TextView)mainAct.findViewById(R.id.SpeedNum)).setText(Float.toString(record.getSpeed()));
		
		String latLonStr="("+Double.toString(record.getLat())+","+Double.toString(record.getLon())+")";
		((TextView)mainAct.findViewById(R.id.LocCoordStr)).setText(latLonStr);
		
		//Updata data series and repaint graph
		XYSeries series = mainAct.dataset.getSeriesAt(0);
		series.add((record.getTime()-Record.getFirstTime())*0.001, record.getSpeed()*2.23693629);
		if(mainAct.mChartView!=null){
			Log.d(MocGPS.TAG, "Before repaint!");
			mainAct.mChartView.repaint();
		}
		//move the car on map
		mMapController.setZoom(22);
		int lat = (int) (location.getLatitude() * 1E6);
		int lon = (int) (location.getLongitude() * 1E6);
		GeoPoint point = new GeoPoint(lat, lon);
		mMapController.setCenter(point);
		OverlayItem item = new OverlayItem(point, "Current Location", "");
		//Log.d(MocGPS.TAG, Integer.toString(mOverlay.size(), 10));
		mOverlay.removeOldItems();
		mOverlay.addItem(item);
		//record to tracefile
		String recordStr=Integer.toString(lat)+","+Integer.toString(lon)+","+record.getTime()+","+record.getSpeed();
		if(mainAct.fileManager.write(recordStr)){
			Log.d(MocGPS.TAG, "Record Saved: "+recordStr);
		}
	}

	public void onProviderDisabled(String provider) {
		// TODO Auto-generated method stub

	}

	public void onProviderEnabled(String provider) {
		// TODO Auto-generated method stub

	}

	public void onStatusChanged(String provider, int status, Bundle extras) {
		// TODO Auto-generated method stub

	}

}
